<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="userId" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="userId"></el-input>
    </el-form-item>
    <el-form-item label="联系人" prop="name">
      <el-input v-model="dataForm.name" placeholder="联系人"></el-input>
    </el-form-item>
    <el-form-item label="联系电话" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="联系电话"></el-input>
    </el-form-item>
    <el-form-item label="所管理的景点名称" prop="manageSceneName">
      <el-input v-model="dataForm.manageSceneName" placeholder="所管理的景点名称"></el-input>
    </el-form-item>
    <el-form-item label="所管理的景点地址" prop="manageSceneAddress">
      <el-input v-model="dataForm.manageSceneAddress" placeholder="所管理的景点地址"></el-input>
    </el-form-item>
    <el-form-item label="所管理的景点描述信息" prop="manageSceneDescription">
      <el-input v-model="dataForm.manageSceneDescription" placeholder="所管理的景点描述信息"></el-input>
    </el-form-item>
    <el-form-item label="申请时间" prop="applyTime">
      <el-input v-model="dataForm.applyTime" placeholder="申请时间"></el-input>
    </el-form-item>
    <el-form-item label="申请状态,0未处理,1成功,2失败" prop="state">
      <el-input v-model="dataForm.state" placeholder="申请状态,0未处理,1成功,2失败"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          name: '',
          phone: '',
          manageSceneName: '',
          manageSceneAddress: '',
          manageSceneDescription: '',
          applyTime: '',
          state: ''
        },
        dataRule: {
          userId: [
            { required: true, message: 'userId不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '联系人不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '联系电话不能为空', trigger: 'blur' }
          ],
          manageSceneName: [
            { required: true, message: '所管理的景点名称不能为空', trigger: 'blur' }
          ],
          manageSceneAddress: [
            { required: true, message: '所管理的景点地址不能为空', trigger: 'blur' }
          ],
          manageSceneDescription: [
            { required: true, message: '所管理的景点描述信息不能为空', trigger: 'blur' }
          ],
          applyTime: [
            { required: true, message: '申请时间不能为空', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '申请状态,0未处理,1成功,2失败不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/haiyou/scenemanagerapply/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.sceneManagerApply.userId
                this.dataForm.name = data.sceneManagerApply.name
                this.dataForm.phone = data.sceneManagerApply.phone
                this.dataForm.manageSceneName = data.sceneManagerApply.manageSceneName
                this.dataForm.manageSceneAddress = data.sceneManagerApply.manageSceneAddress
                this.dataForm.manageSceneDescription = data.sceneManagerApply.manageSceneDescription
                this.dataForm.applyTime = data.sceneManagerApply.applyTime
                this.dataForm.state = data.sceneManagerApply.state
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/haiyou/scenemanagerapply/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'name': this.dataForm.name,
                'phone': this.dataForm.phone,
                'manageSceneName': this.dataForm.manageSceneName,
                'manageSceneAddress': this.dataForm.manageSceneAddress,
                'manageSceneDescription': this.dataForm.manageSceneDescription,
                'applyTime': this.dataForm.applyTime,
                'state': this.dataForm.state
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
